<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
</head>
<body>
<script>

    /*
    content-type: text/html
    查询字符串参数：
    id: 123
    name: 张三
    */
    function get1() {
        fetch("/clock?id=123&name=张三")
            .then(res => res.json())
            .then(res => { console.log(res)})
            .catch(err => {console.log(err)})
    }

    /*
    异常：TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.
    使用 GET/HEAD 方法的请求不能有正文(body)
    */
    function get2() {
        let id = 12312;
        let name = "站撒";
        fetch("/clock",{method:"GET",body:JSON.stringify({id, name})})
            .then(res => res.json())
            .then(res => { console.log(res)})
            .catch(err => {console.log(err)})
    }

    /*
    Content-Type: text/plain;charset=UTF-8
    请求负载
    {id: 12312, name: "站撒"}
    id: 12312
    name: "站撒"
    */
    function post1() {
        let id = 12312;
        let name = "站撒";
        fetch("/clock",{method:"POST",body:JSON.stringify({id, name})})
            .then(res => res.json())
            .then(res => { console.log(res)})
            .catch(err => {console.log(err)})
    }


    /*
    Content-type: application/json;charset=urf-8
    请求负载
    {id: 12312, name: "站撒"}
    id: 12312
    name: "站撒"
    */
    function post2() {
        let id = 12312;
        let name = "站撒";
        fetch("/clock",{method:"POST",headers:{"Content-type": "application/json;charset=urf-8"},body:JSON.stringify({id, name})})
            .then(res => res.json())
            .then(res => { console.log(res)})
            .catch(err => {console.log(err)})
    }

    /*
       Content-type: application/x-www-form-urlencoded
       表单数据
       {"id":12312,"name":"站撒"}:
    */
    function post3() {
        let id = 12312;
        let name = "站撒";
        fetch("/clock",{method:"POST",headers:{"Content-type": "application/x-www-form-urlencoded",},body:JSON.stringify({id, name})})
            .then(res => res.json())
            .then(res => { console.log(res)})
            .catch(err => {console.log(err)})
    }

    post3()

</script>

</body>
</html>
